//
//  CHPopView.m
//  SZTZ
//
//  Created by 陈浩 on 2017/3/7.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHPopView.h"
#import "CHTableViewCell.h"
#import "CHSectionModel.h"
@interface CHPopView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UIButton *coverBtn;

@end
@implementation CHPopView



-(void)presentPopViewToView:(UIView *)view{
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIButton *coverBtn = [[UIButton alloc] initWithFrame:window.bounds];
    [coverBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [window addSubview:coverBtn];
    coverBtn.backgroundColor = [UIColor clearColor];
//    coverBtn.alpha = 0.2;
    self.coverBtn = coverBtn;
    [window addSubview:self];
    
}






-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self initView];
    }
    return self;
    
    
}


-(void)initView{
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds];
//    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.bounces = NO;
    tableView.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1];
    [self addSubview:tableView];

}



-(void)dismiss{
    
    [self.coverBtn removeFromSuperview];
    [self removeFromSuperview];
}



#pragma mark - UITableViewDatasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datas.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CHTableViewCell *cell = [CHTableViewCell cellForTableView:tableView andWidth:self.bounds.size.width];
    cell.backgroundColor = [UIColor colorWithRed:229/255.0 green:229/255.0 blue:229/255.0 alpha:1];;

    if (_isSection) {
       CHSectionModel *model = self.datas[indexPath.row];
       cell.textView.text = model.codevalue;
    }else{
       CHDateModel *model = self.datas[indexPath.row];
       cell.textView.text = [NSString stringWithFormat:@"%@年%@月",model.nf,model.yf];
    }
   
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.delegate respondsToSelector:@selector(popViewDidSelectRowAtIndex:)]) {
        [self.delegate popViewDidSelectRowAtIndex:indexPath.row];
    }
    [self dismiss];
}

//
//补全分割线
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell  respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
    {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
